/* Exercise 4.6 Second Clock */ /* Created David Miles April 2006 */ /* Updated Ben Rowland Febuary 2014 */ #include // CONFIG1 #pragma config FOSC = HS // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config VCAPEN = OFF // Voltage Regulator Capacitor Enable (All VCAP pin functionality is disabled) #pragma config PLLEN = OFF // PLL Enable (4x PLL disabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) #define _XTAL_FREQ 19660800 // Defines the hardware crystal frequency allowing the delay function to work correctly //A value for each bit in PORTA. We can feed in the number of the LED and it will give us the value to put into A const unsigned char enable [4] = { 1, 2, 4, 8 } ; //These are the patterns for the LEDs which were worked out from the datasheet. //Note that to light a LED the bit on PORTB must be low I can use this array to convert from a digit to the 7 segments needed const unsigned char patterns [10] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x83, 0xf8, 0x80, 0x98 } ; // 0 1 2 3 4 5 6 7 8 9 #define DISPLAY_SIZE 4 //Number of LEDs in our display unsigned char segments [DISPLAY_SIZE] ; //Segment patterns for our LEDs unsigned char led_counter = 0 ; //Counter used by our refresh unsigned char counter = 0; //Timer interrupt counter int count = 0; //Our counter value int divisor = 0; //Interrupt divide count value void setup_hardware (void) { ANSELA = 0x00; //Set PORTA to Digital Mode - Defaults to analogue mode ANSELB = 0x00; //Set PORTB to Digital Mode - Defaults to analogue mode OPTION_REG = 0xC0; TRISA = 0xF0; //Set bits 0-3 PORTA to be configured as an output TRISB = 0x00; //Set all bits of PORTB to be configured as an output INTCON = 0b10100000; //Configure INTCON register to enable timer interrupts OPTION_REG = 0b11000100; //Configure the timer to use the prescaler 1:32 - 19660800 / 4 / 32 / 256 = 600Hz } //Each time refresh is called it sets the display for a led and moves on to the next void refresh ( void ) { LATA = 0 ; //Turn off all the LEDS LATB = segments[led_counter]; //Set segments for the led LATA = enable[led_counter]; //Turn the led on led_counter = led_counter + 1 ; //Move on to the next led if ( led_counter == DISPLAY_SIZE ) //see if we fell off the end { led_counter = 0 ; } } //Display just loads the pattern into the segment. refresh will read it later void display ( unsigned char digit, unsigned char pos ) { segments[pos] = patterns[digit]; } //TMR0 Overflow handler void tmrHandler( void ) { refresh(); divisor++; if (divisor == 20) //update count every 10th of a second { count++; divisor = 0; } } //Allows a numeric variable to be printed to the 4 x 7-seg LEDs void display_value ( int value ) { display ( value % 10, 3 ) ; //display the units value = value / 10 ; display ( value % 10, 2 ) ; //display the tens value = value / 10 ; display ( value % 10, 1 ) ; //display the hundreds value = value / 10 ; display ( value % 10, 0 ) ; //display the thousands } //This is the interrupt handler which is called when the PIC detects an interrupt //It checks the status bits to find out who caused the interrupt and then calls that handler void interrupt isr( void ) { if(INTCON & 0b00000100) //if the timer has overflowed bit 2 of INTCON is set high { INTCON = INTCON & 0b11111011; //Clear bit 2 to turn off this interrupt counter = counter + 1; //Increment interrupt counter if ( counter > 2 ) //If counter is greater than 2 { counter = 0; //Reset counter to 0 tmrHandler(); //Call the interrupt handler function } } } int main (void) { setup_hardware(); while (1) //Loop forever { display_value (count); //Send new count to LEDs } return 0; }